﻿Imports System.Data.SqlClient
Public Class Employeeinfo
    Dim cmd As New SqlCommand
    Private Sub Employeeinfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        refreshdata()
    End Sub
    Public Sub refreshdata()
        grdemployee.Rows.Clear()
        cmd.Connection = cn
        cmd.CommandText = "select * from tblemployee where Name like N'%" & txtsearch.Text & "%' OR EmpID like N'%" & txtsearch.Text & "%' OR phone like N'%" & txtsearch.Text & "%'"
        Dim rd As SqlDataReader = cmd.ExecuteReader
        While rd.Read = True
            Me.grdemployee.Rows.Add(rd(0), rd(1), rd(2), rd(3), rd(4), rd(5), rd(6), rd(7), rd(8), rd(9))
        End While
        rd.Close()
        radname.Checked = True
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

        Dim nfrm As New EmployeeEdotor
        nfrm.btnupdate.Enabled = False
        If nfrm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            refreshdata()

        End If
    End Sub

    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        If Me.grdemployee.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row As DataGridViewRow = Me.grdemployee.SelectedRows(0)
        Dim frm As New EmployeeEdotor
        frm.btnaddeditor.Enabled = False
        frm.txtid.Text = row.Cells("id").Value
        frm.txtcode.Text = row.Cells("code").Value
        frm.txtname.Text = row.Cells("empname").Value
        frm.txtgender.Text = row.Cells("gender").Value
        frm.txtdob.Text = row.Cells("dob").Value
        frm.txtpob.Text = row.Cells("pob").Value
        frm.txtphon.Text = row.Cells("Phone").Value
        frm.txtsalary.Text = row.Cells("salary").Value
        frm.txtposition.Text = row.Cells("Possition").Value
        frm.txtdescription.Text = row.Cells("description").Value
        frm.ShowDialog()
        refreshdata()
    End Sub

    Private Sub btndelet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelet.Click
        If Me.grdemployee.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        If MessageBox.Show("Are you want to delete", "Delete Confirme", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        For Each row In grdemployee.SelectedRows
            Dim id = row.Cells("id").Value
            cmd.Connection = cn
            cmd.CommandText = "delete from tblemployee where EmpID= " & id
            cmd.ExecuteNonQuery()
        Next
        refreshdata()
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        refreshdata()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    End Sub

    Private Sub grdemployee_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    End Sub
End Class